package com.hearwego;


import java.util.ArrayList;
import java.util.Dictionary;

import com.utilities.Utilities;
import com.utilities.Utils;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {

	public Dialog progress;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		
		new AsyncUpdate().execute();
		
		
		Button bt_cat = (Button) findViewById(R.id.bt_categorias);
		Button bt_mapa = (Button) findViewById(R.id.bt_mapa);
		Button bt_real = (Button) findViewById(R.id.bt_realidad);
		Button bt_ruta = (Button) findViewById(R.id.bt_ruta);
		
		bt_cat.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent_cat = new Intent(MainActivity.this, ActivityCategorias.class);
				startActivity(intent_cat);
			}
		});
		
		bt_mapa.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent_mapa = new Intent(MainActivity.this, ActivityMapa.class);
				startActivity(intent_mapa);
			}
		});
		
		bt_real.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent_ar = new Intent(MainActivity.this, ActivityAugmentedReality.class);
				startActivity(intent_ar);
			}
		});
		
		bt_ruta.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent_ruta = new Intent(MainActivity.this, ActivityRutaTematica.class);
				startActivity(intent_ruta);
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.miviaje:
			Intent intent_miviaje = new Intent(MainActivity.this, ActivityMiViaje.class);
			startActivity(intent_miviaje);
			break;
			
		case R.id.camara:		
			Intent intent_camara = new Intent(MainActivity.this, ActivityCamara.class);
			startActivity(intent_camara);
			break;

		default:
			break;
		}
		
		
		return super.onOptionsItemSelected(item);
	}
	
	
	
	public class AsyncUpdate extends AsyncTask<Void, Integer, Void>{
		
		ArrayList<Dictionary<String, String> > listaupdate = null;
//		ArrayList<Dictionary<String, String> > Banners = null;
//		int isFirstDownload = 0;
		int isDownloaded = 0;
		int isNetwork = 0;
		int isOk = 0;
		SharedPreferences preferences;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progress = ProgressDialog.show(MainActivity.this, "","Cargando");
		}

		@Override
		protected Void doInBackground(Void... params) {
			preferences = MainActivity.this.getSharedPreferences("prefs", MODE_PRIVATE);
			
			
			//Luego valido la version
			if(preferences.getString("dbversion", "0").equals("0")){
				//primera descarga
				isDownloaded = 0;
//				if(Utilities.isNetwork()){
//					isNetwork = 1; 
//					String strqry = "select version,fecha from ctrl_updates";
//					
//					listaupdate = Utilities.qry(MainActivity.this, strqry.replace(" ", "%20"));
//				}else{
//					isNetwork = 0;
//				}
			}else{
				//actualizacion
				isDownloaded = 1;
//				if(Utilities.isNetwork()){
//					isNetwork = 1; 
//					String strqry = "select version,fecha from ctrl_updates";
//					listaupdate = Utilities.qry(MainActivity.this, strqry.replace(" ", "%20"));
//				}else{
//					isNetwork = 0;
//				}
			}
			
//		    String qry  = "select a.* from eventos_banners a inner join banners_catalogo b on a.ban_idr = b.ban_idr where a.eve_idr = 1 and b.ban_nombre = 'BANNER HOME' ORDER BY b.ban_nombre";
//			Banners = Utilities.qry(getApplicationContext(), qry);
			
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progress.dismiss();

			if(isDownloaded == 0){
				Intent goUpdate = new Intent(MainActivity.this, UpdateDB.class);
				startActivity(goUpdate);
				
				
			}
//			if((isFirstDownload == 1)&&(isNetwork == 0)){
//				Log.i("1", "(isFirstDownload == 1)&&(isNetwork == 0)");
//				AlertDialog.Builder ad = new AlertDialog.Builder(Principal.this);  
//				ad.setTitle(Principal.this.getString(R.string.msg_sin_internet));  
//				ad.setMessage(Principal.this.getString(R.string.msg_descarga_reintentar));   
//				ad.setPositiveButton(Principal.this.getString(R.string.msg_si), new DialogInterface.OnClickListener() {  
//					public void onClick(DialogInterface dialog, int id) {  
//						dialog.dismiss();
//						new AsyncUpdate().execute();
//					}  
//				}).setNegativeButton(Principal.this.getString(R.string.msg_no), new DialogInterface.OnClickListener() {
//					@Override
//					public void onClick(DialogInterface dialog, int which) {
//						dialog.dismiss();
//						finish();
//					}
//				});  
//				ad.show();
//			}else if((isFirstDownload == 1)&&(isNetwork == 1)){
//				Log.i("2", "(isFirstDownload == 1)&&(isNetwork == 1)");
//				if(listaupdate.size() > 0){
//					AlertDialog.Builder ad = new AlertDialog.Builder(Principal.this);  
//					ad.setTitle(Principal.this.getString(R.string.msg_descarga_titulo));  
//					ad.setMessage(Principal.this.getString(R.string.msg_descarga));
//					ad.setPositiveButton(Principal.this.getString(R.string.msg_si), new DialogInterface.OnClickListener() {  
//						public void onClick(DialogInterface dialog, int id) {  
//							dialog.dismiss();
//							Intent goUpdate = new Intent(Principal.this, UpdateDB.class);
//							goUpdate.putExtra("dbversion", ""+listaupdate.get(0).get("version").toString());
//							startActivity(goUpdate);
//						}  
//					}).setNegativeButton(Principal.this.getString(R.string.msg_no), new DialogInterface.OnClickListener() {
//
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							dialog.dismiss();
//							finish();
//						}
//					});  
//					ad.show();
//				}else{
//					AlertDialog.Builder ad = new AlertDialog.Builder(Principal.this);  
//					ad.setTitle("Error");  
//					ad.setMessage("Error del servidor");
//					ad.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {  
//						public void onClick(DialogInterface dialog, int id) {  
//							finish();
//						}  
//					});  
//					ad.show();
//					
//				}
//				
//
//			}else if((isFirstDownload == 0)&&(isNetwork == 0)){
//				Log.i("3", "(isFirstDownload == 0)&&(isNetwork == 0)");
//			}else if((isFirstDownload == 0)&&(isNetwork == 1)){
//				Log.i("4", "(isFirstDownload == 0)&&(isNetwork == 1)");
//				if(listaupdate.size() > 0){
//					if((!preferences.getString("dbversion", "0").equals(""+listaupdate.get(0).get("version").toString()))){
//						Log.i("4", "ACTUALIZACION");
//						AlertDialog.Builder ad = new AlertDialog.Builder(Principal.this);  
//						ad.setTitle(Principal.this.getString(R.string.msg_descarga_titulo));  
//						ad.setMessage(Principal.this.getString(R.string.msg_descarga_act));   
//						ad.setPositiveButton(Principal.this.getString(R.string.msg_si), new DialogInterface.OnClickListener() {  
//							public void onClick(DialogInterface dialog, int id) {  
//								dialog.dismiss();
//								Intent goUpdate = new Intent(Principal.this, UpdateDB.class);
//								goUpdate.putExtra("dbversion", ""+listaupdate.get(0).get("version").toString());
//								startActivity(goUpdate);
//							}  
//						}).setNegativeButton(Principal.this.getString(R.string.msg_no), new DialogInterface.OnClickListener() {
//
//							@Override
//							public void onClick(DialogInterface dialog, int which) {
//								dialog.dismiss();
//							}
//						});  
//						ad.show();
//					}else{
//						Log.i("4", "NADA");
//					}
//				}else{
//					AlertDialog.Builder ad = new AlertDialog.Builder(Principal.this);  
//					ad.setTitle("Error");  
//					ad.setMessage("Error del servidor");
//					ad.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {  
//						public void onClick(DialogInterface dialog, int id) {  
//							finish();
//						}  
//					});  
//					ad.show();
//				}
//				
//
//				
//			}//if first download
		}//post-execute
	}
	
	
	
	

}
